
package Test1.Thermometry;

import com.sun.jna.Structure;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by panting6 on 2021/1/25.
 */
public class dataAnalysis {

    /**
     * 传入获取的文件地址，输出其中的每帧全屏测温数据保存在 e盘
     *
     * @param realdata 传入获取到的源数据文件地址，如 "C:\\Users\\qjh\\Desktop\\realdata20212202172243.date"
     * @param widthSize 每帧数据宽度
     * @param heightSize 每帧数据高度
     * @throws IOException
     *
     *
     */
    public void getFullScreenTemperatureData(String realdata, int widthSize, int heightSize) throws IOException {

        //将数据转成 byte数组
        byte[] fileByte = toByteArray(realdata);

        List<byte[]> getAllFrameData = getSmallByteFromData(fileByte);
        List<Float[]> floatList = new ArrayList<>();

        //总数据 宽度*高度
        int allNum = widthSize * heightSize;

        //每帧数据长度
        Float[] floats = new Float[allNum];

        if (allNum > 0) {
            for (int i = 0; i < getAllFrameData.size(); i++) {
                Float getFloat = byteArray2Float(getAllFrameData.get(i));
                floats[i % allNum] = getFloat;
                if (i % allNum == 0 && i > 0) {
                    floatList.add(floats);
                }
            }
        }
        for (int i = 0; i < floatList.size(); i++) {
            float2File(floatList.get(i), ".\\pic\\" + i + ".csv", widthSize);
        }
    }

    /**
     * 读取本地文件到数组中
     * @param filename 本地文件
     * @return 返回读取到的数据到 byte数组
     * @throws IOException
     */
    public byte[] toByteArray(String filename) throws IOException {
        File file = new File(filename);
        if (!file.exists()) {
            throw new FileNotFoundException(filename);
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

        try {
            byte[] buffer = new byte[1024];
            int len;
            while (-1 != (len = in.read(buffer, 0, buffer.length))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            bos.close();
            in.close();
        }
    }

    /**
     * 将float数组数据写入文件
     *
     * @param floatByte    float数组
     * @param saveFileName 输出文件名
     * @param widthSize 数据宽度
     * @throws IOException
     */
    public void float2File(Float[] floatByte, String saveFileName, int widthSize) throws IOException {

        //数据输入csv表格
        File saveFile = new File(saveFileName);
        FileWriter writer = new FileWriter(saveFile);

        int count = 0;

        for (int i = 0; i < floatByte.length; i++) {

            // 挨个将floatList中的数据写入文件
            writer.write(floatByte[i].toString());
            writer.write(",");
            count++;

            //每160个数据换行
            if (widthSize != 0) {
                if (count % widthSize == 0) {
                    writer.write('\r');
                    writer.write('\n');
                    continue;
                }
            }
            writer.flush();
        }
        writer.close();
    }

    /**
     * 按每4个数据组成一个数组的List集合
     *
     * @param test1 元数据转换的数组
     * @return 长度为4的数组集合 List<byte[4]>
     */
    public List<byte[]> getSmallByteFromData(byte[] test1) {

        List<byte[]> result = new ArrayList<>();

        //定义四个字节长度的byte数组
        byte[] temp = new byte[4];
        for (int i = 0; i < test1.length; i++) {

            if (i % 4 == 0) {
                temp[3] = test1[i];
            } else if (i % 4 == 1) {
                temp[2] = test1[i];
            } else if (i % 4 == 2) {
                temp[1] = test1[i];
            } else if (i % 4 == 3) {
                temp[0] = test1[i];
                result.add(temp);
                temp = new byte[4];
            }

        }

        return result;
    }

    /**
     * 输入四个字节的数组转成float
     *
     * @param temp 长度为4的byte数组
     * @return 长度为4的byte数组转换的 Float
     */
    public Float byteArray2Float(byte[] temp) {

        int accum = 0;

        accum = accum | (temp[3] & 0xff) << 0;
        accum = accum | (temp[2] & 0xff) << 8;
        accum = accum | (temp[1] & 0xff) << 16;
        accum = accum | (temp[0] & 0xff) << 24;

        // int 转 float
        Float result = Float.intBitsToFloat(accum);
        return result;
    }
}
